/*
 	PureMVC UnifiedLoader Utility
 	Copyright (c) 2008 neil manuell
  	Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package org.puremvc.as3.multicore.utilities.unifiedloader.loader
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	import org.puremvc.as3.multicore.utilities.unifiedloader.errors.FactoryReturnError;
	import org.puremvc.as3.multicore.utilities.unifiedloader.errors.LoadQueueError;
	import org.puremvc.as3.multicore.utilities.unifiedloader.interfaces.*;

	/**
	 * 
	 * This class is the data object that holds all the load information for each asset. 
	 * It is stored and manipulated by the Loader, and passed to the client, which in turn sets 
	 * its asset, that cna then be retrived.
	 * 
	 */
	internal class LoadRequest extends EventDispatcher implements IAssetInfo, ILoaderClientInfo
	{
		private var mAsset:*;
		private var mFileType:String;
		private var mURLReq:URLRequest;
		private var mAssetType:String;
		private var mLoaderContext:Object;
		private var mStore:Boolean;
		private var mPriority:int;
		private var mSetLoaderType:String;
		private var mDeconstructor:IDeconstruct;
		private var mStatus:String;
		private var mLoaderCallBack:Function;

		public function LoadRequest(url:String):void
		{
			this.url = url;
			this.store = true;
			this.status = AssetInfoStatus.READY;
		}
		
		public function get fileType():String
		{
			return mFileType;
		}
		
		public function get url():String
		{
			return mURLReq.url;
		}
		
		public function set url(param:String):void
		{
			mFileType = getExtention(param);
			mURLReq = new URLRequest(param);
		}
		
		public function get priority():int
		{
			return mPriority;
		}
		
		public function set priority(param:int):void
		{
			mPriority = param;
			dispatchEvent( new Event(Event.CHANGE));
		}
		
		public function get store():Boolean
		{
			return mStore;
		}
		
		public function set store(param:Boolean):void
		{
			mStore = param;
		}
		
		public function get status():String
		{
			return mStatus;
		}
		
		public function set status(param:String):void
		{
			mStatus = param;
		}
		
		public function set context(param:Object):void
		{
			mLoaderContext  = param;
		}
		
				
		public function set deconstructor(param:IDeconstruct):void
		{
			mDeconstructor  = param;
		}
		
		public function get clientType():String
		{
			return mSetLoaderType;
		}
		
		public function set clientType(param:String):void
		{
			mSetLoaderType  = param;
		}
		
		public function completedCallBack():void
		{
			if(mLoaderCallBack != null)mLoaderCallBack(this);
		}
		
		public function set loaderCallBack(param:Function):void
		{
			mLoaderCallBack  = param;
		}
		
		public function getAsset():*
		{
			
			switch(true){
				
				case(status == AssetInfoStatus.LOADING):
					throw new LoadQueueError (LoadQueueError.LOADING_ITEM);
					break;
					
				case(status == AssetInfoStatus.QUEUED):
					throw new LoadQueueError (LoadQueueError.QUEUED_ITEM);
					break;
					
				case(status == AssetInfoStatus.READY):
					throw new LoadQueueError (LoadQueueError.READY_ITEM);
					break;
					
				case(mAsset is ICreate):
					return ICreate(mAsset).create("asset");
					
				default:
					return mAsset;
				
			}
			
		}
		
		public function setAsset(asset:*):void
		{
			mAsset = asset;
		}
				
		public function get urlRequest():URLRequest
		{
			return mURLReq;
		}
		
		public function get loaderContext():LoaderContext
		{
			return mLoaderContext as LoaderContext
		}
		
		public function get soundLoaderContext():SoundLoaderContext
		{
			return mLoaderContext as SoundLoaderContext
		}
		
		public function relayEvent(event:Event):void
		{
			dispatchEvent(event);
		}
		
		public function create(id:String = "", asClass:Boolean=false):*
		{
			var asset:*
			
			switch(true)
			{
									
				case(mAsset is ICreate):
					return ICreate(mAsset).create(id, asClass);
					

				default:
					throw new FactoryReturnError(FactoryReturnError.NOT_A_FACTORY);
					
			}
		
				
		}
		
		public function deconstruct():void
		{
			if(mDeconstructor)
			{
				mDeconstructor.deconstruct();
			}
			
			mAsset = null;
			
		}
		
		private function getExtention(url:String):String
		{
			var urlSplit:Array;
			
			urlSplit = url.split(".");
			
			return urlSplit[urlSplit.length-1]
		}
		
		
		

	}
}